﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zebra.Utility;

namespace Zebra.WinFramework
{
    public partial class LoginForm : FormUC
    {
        CustomErrorProvider myErrorProvider = new CustomErrorProvider();

        private bool m_bLayoutCalled = false;
        private DateTime m_dt;

      //  private Main _mainform = null;

        public LoginForm()
        {
            InitializeComponent();
        }

        private void AuthenticateLogin()
        {
            if (myErrorProvider.CheckAndShowSummaryErrorMessage() == true)
            {
                //Validate User
                
                //Save user login

                //Load User Preference

                //Show main form
                this.Hide();
               
                //MainUCForm main = new MainUCForm();   //Should call the Zebra MainForm instead but how??
                //main.Show();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            AuthenticateLogin();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool bHandled = false;

            switch (keyData)
            {
                case Keys.Enter:
                    AuthenticateLogin();
                    break;
            }
            return bHandled;
        }

        /// <summary>
        /// This would hide the LOGIN form after success login and go into Main form.
        /// </summary>
        private void OpenMainForm()
        {
            //LoginForm parentFrm = this;
            //if (_mainform == null)
            //    _mainform = new Main(ref parentFrm);
            //if (!_mainform.Visible)
            //    _mainform.Show();
            //else
            //    _mainform.Load += new System.EventHandler(_mainform.Main_Load);
            //this.Visible = false;
        }

        private void LoginForm_Layout(object sender, LayoutEventArgs e)
        {
            if (m_bLayoutCalled == false)
            {
                m_bLayoutCalled = true;
                m_dt = DateTime.Now;
                //if (SplashScreen.SplashForm != null)
                //    SetOwnerCrossThread( this);
                this.Activate();
                SplashScreen.CloseForm();
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            myErrorProvider.Controls.Add((object)txtId, "User Id");
            myErrorProvider.Controls.Add((object)txtPassword, "Password");
        }

    }
}
